package com.whut.tool;

import java.util.HashMap;
import java.util.Map;

public class KeyUtil {
    private static final int SIZE = 19;
    private static final char[] sourceChars = new char[]{
            '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e',
            'f', 'g', 'h', 'i'
    };
    private static final char[] destChars = new char[]{
            'a', 'b', 'c', 'd', 'e',
            'f', 'g', 'h', 'k', 'm',
            'n', 'p', 'r', 's', 't',
            'w', 'x', 'y', 'z'
    };

    private static final Map<Character, Character> sourceList = new HashMap<>(SIZE);
    private static final Map<Character, Character> destList = new HashMap<>(SIZE);

    static {
        for (int i = 0; i < SIZE; i++) {
            sourceList.put(sourceChars[i], destChars[i]);
            destList.put(destChars[i], sourceChars[i]);
        }
    }

    public static String transKey(Integer num) {
        char[] source = Integer.toString(num, sourceList.size()).toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : source) {
            sb.append(sourceList.getOrDefault(c, 'A'));
        }
        return sb.toString();
    }

    public static Integer tranNum(String key) {
        char[] source = key.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : source) {
            sb.append(destList.getOrDefault(c, '0'));
        }
        return Integer.valueOf(sb.toString(), SIZE);
    }

    // public static void main(String[] args) {
    //     List<Integer> list = IntStream.range(0, 1000)
    //             .mapToObj(KeyUtil::transKey)
    //             .peek(System.out::print)
    //             .peek(t -> System.out.print(" "))
    //             .map(KeyUtil::tranNum)
    //             .peek(System.out::println)
    //             .collect(Collectors.toList());
    // }
}
